package com.norriekoelle.virtualsensors.client;

import android.app.Activity;
import android.util.Log;

import com.norriekoelle.virtualsensors.server.WifiClient;
import com.norriekoelle.virtualsensors.shared.Receiver;

/**
 * Abstract activity to allow {@link HotspotActivity} and {@link BookshelfActivity} to share the 
 * WifiClient.
 * 
 * @author Lauren Norrie (norrielm)
 */
public abstract class ClientActivity extends Activity {
    
	protected static final String LOG = "ClientActivity";

	WifiClient connectionManager;
	
	private final Receiver<String> messageReceiver = new Receiver<String>() {
		@Override
		public void accept(String message) {
//			handleMessage(message);
			Log.d("WifiClient", message);
		}
	};
	private Receiver<String> commandHandler = new Receiver<String>() {
		@Override
		public void accept(String serverCommand) {
			handleCommand(serverCommand);
		}
	};
	private Runnable disconnectedCallback = new Runnable() {
		@Override
		public void run() {
			handleDisconnect();
		}
	};
	private Runnable exitingCallback = new Runnable() {
		@Override
		public void run() {
			exit();
		}
	};
	private Runnable noNetworkCallback = new Runnable() {
		@Override
		public void run() {
			Log.w(LOG, "No network callback called. Ensure server is connected.");
//			AlertDialog.Builder a = new AlertDialog.Builder(ClientActivity.this);
//        	a.setMessage("Please connect to a wifi network and try again.");
//        	a.setTitle("Server not found.");
//			a.create();
//			a.show();
		}
	};

	public ClientActivity() {
		connectionManager = new WifiClient(disconnectedCallback, exitingCallback, noNetworkCallback,
        		messageReceiver, commandHandler);
	}
	
    public abstract void handleMessage(String message);
    // TODO(norrielm): Remove this log state method.
    public abstract void doLog(boolean b);
    public abstract void handleCommand(String command);
	public abstract void handleDisconnect();
	public abstract void exit();
}
